package com.vm.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.DetailActivity;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.myview.GridViewDown;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.SoftDataUtil;
import com.wwtscapp.apk.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月2日 上午10:32:21]
 * @Version: [v1.0]
 * 
 */
@SuppressLint("InflateParams")
public class FoundAdapter extends BaseAdapter {

	private Context mContext;
	// private ImageLoader mImageLoader;
	private List<ActiveSoft> listSofts;
	boolean isFirstZero = true;

	/**
	 * 
	 */
	public FoundAdapter(Context context, List<ActiveSoft> lists) {
		this.mContext = context;
		this.listSofts = lists;
	}

	@Override
	public int getCount() {
		return listSofts.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.found_gv_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */

			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);

			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			viewHolder.tv_download_num = (TextView) convertView
					.findViewById(R.id.tv_download_num);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			if (isFirstZero) {
				isFirstZero = false;
			} else {
				return convertView;
			}

		} else {
			isFirstZero = true;
		}

		Log.i("FoundAdapter", "position:" + position);

		ActiveSoft activeSoft = listSofts.get(position);

		viewHolder.tv_softName.setTag(activeSoft.getSoftId());
		// 名称

		if (activeSoft.getName() != null) {
			String softName = StringUtil.getShotName(activeSoft.getName());
			viewHolder.tv_softName.setText(softName);
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String shotSize = decimalFormat.format(size);// format 返回的是字符串
			viewHolder.tv_softSize.setText(shotSize + "M");
		}

		if (activeSoft.getDownload() != null) {
			String download = SoftDataUtil.getSoftDownload(activeSoft.getDownload());
			viewHolder.tv_download_num.setText(download);
		}
		String url = VMConst.URL + activeSoft.getImageUrl();
		ImageLoader.getInstance().displayImage(url, viewHolder.iv_softImage,
				MyDisplayImageOptions.getInstance());

		convertView.setOnClickListener(gd_listener);

		new GridViewDown(mContext, convertView, activeSoft);

		activeSoft = null;

		return convertView;
	}

	OnClickListener gd_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(v.getContext(), DetailActivity.class);
			TextView textView = (TextView) v.findViewById(R.id.tv_softName);
			String softId = textView.getTag().toString();
			intent.putExtra("softId", softId);
			v.getContext().startActivity(intent);
		}
	};

	static class ViewHolder {
		TextView tv_softName;
		ImageView iv_softImage;
		TextView tv_softSize;
		TextView tv_download_num;

	}

}
